package com.android.delivery.adapter.siji

import android.widget.ImageView
import com.android.delivery.R
import com.android.delivery.activity.OrderSettleActivity
import com.android.delivery.entity.Logistics
import com.android.delivery.entity.OrderState
import com.android.delivery.util.CopyUtil
import com.android.delivery.util.DateFormatUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder

/**
 * @author: mqwang
 * @create: 3/17/21 8:30 AM
 * @Description:
 */
class OrderAdapter : BaseQuickAdapter<Logistics, BaseViewHolder>(R.layout.adapter_order) {
    private var type: Int = 0
    override fun convert(helper: BaseViewHolder?, item: Logistics) {
        if (type > 0) {
            helper!!.setGone(R.id.iv_select, false)
            helper.setGone(R.id.tv_order_state, true)
        } else {
            helper!!.setGone(R.id.iv_select, true)
            helper.setGone(R.id.tv_order_state, false)
        }

        if (item.type == OrderSettleActivity.TYPE_LINDAN_SEND) {
            helper.setText(R.id.tv_mark, "零担")
        } else if (item.type == OrderSettleActivity.TYPE_CITY_SEND) {
            helper.setText(R.id.tv_mark, "同城")
        } else {
            helper.setText(R.id.tv_mark, "速配")
        }

        helper.setText(R.id.tv_money, String.format("%.2f元", item.payment))

        if (item!!.isSelect) {
            helper!!.setImageDrawable(R.id.iv_select, mContext.resources.getDrawable(R.mipmap.ic_select))
        } else {
            helper!!.setImageDrawable(R.id.iv_select, mContext.resources.getDrawable(R.mipmap.ic_normal))
        }

        if (item.state == OrderState.WAIT_PICK) {
            helper.setText(R.id.tv_order_state, "待揽件")
        } else if (item.state == OrderState.PICK) {
            helper.setText(R.id.tv_order_state, "已取件")
        } else if (item.state == OrderState.WAIT_TRANSPOST) {
            helper.setText(R.id.tv_order_state, "待运输")
        } else if (item.state == OrderState.TRANSPOSTING) {
            helper.setText(R.id.tv_order_state, "运输中")
        } else if (item.state == OrderState.ARRIVED) {
            helper.setText(R.id.tv_order_state, "到达")
        } else if (item.state == OrderState.WAIT_DELIVERY) {
            helper.setText(R.id.tv_order_state, "待派送")
        } else if (item.state == OrderState.DELIVERYING) {
            helper.setText(R.id.tv_order_state, "派送中")
        } else if (item.state == OrderState.SIGNED) {
            helper.setText(R.id.tv_order_state, "已签收")
        } else if (item.state == OrderState.CANCEL) {
            helper.setText(R.id.tv_order_state, "已取消")
        }

        helper.setText(R.id.tv_order_no, "快递单号：" + item.logisticsNo)
        helper.setText(R.id.tv_date, "下单时间：" + DateFormatUtil.formatDate(item.createdTime, "yyyy-MM-dd HH:mm:ss"))
        helper.getView<ImageView>(R.id.iv_copy).setOnClickListener {
            CopyUtil.ClipData(mContext, item!!.logisticsNo)
        }
        helper.setText(R.id.tv_send_name, item.senderName)
        helper.setText(R.id.tv_send_addr, item.senderAddress)
        helper.setText(R.id.tv_receiver_name, item.receiverName)
        helper.setText(R.id.tv_receiver_addr, item.receiverAddress)
    }

    fun selectItem(position: Int) {
        val item = data.get(position);
        item.isSelect = !item.isSelect
        notifyItemChanged(position)
    }

    fun selectAllItem(isSelect: Boolean) {
        for (element in data) {
            element.isSelect = isSelect
        }
        notifyDataSetChanged()
    }

    open fun setType(type: Int) {
        this.type = type;
    }

    /**
     * 获取选中id列表
     */
    fun getSelectIdList(): MutableList<String> {

        var list: MutableList<String> = ArrayList()
        for (element in data) {
            if (element.isSelect) {
                list.add(element.logisticsNo)
            }
        }
        return list

    }
}